<!-- 休药期配置 -->
<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList(),searchPage()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">养殖品种</div>
            <el-select filterable clearable v-model="fishVarietyId" filterable clearable>
              <el-option v-for="(item, index) in varietyList" :label="item.varietyName" :value="item.fishVarietyId"
                :key="index + 'fishVariety'" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">药品名称</div>
            <el-select filterable clearable v-model="materialId" filterable clearable>
              <el-option v-for="(item, index) in materialList" :key="index" :label="item.name" :value="item.materialId" />
            </el-select>
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="162" @add="add" @del="del" />
    </template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => {
            return row.drugAlarmConfigId;
          }
          " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.drugAlarmConfigId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="varietyName" label="养殖品种" />
        <el-table-column prop="materialName" label="药品名称" />
        <el-table-column prop="num" label="用药标准">
          <template v-if="scope.row.drugAlarmConfigId" slot-scope="scope">
            {{ scope.row.amount + scope.row.auxiliaryUnitName + '/' + scope.row.areaUnitStr }}
          </template>
        </el-table-column>
        <el-table-column prop="days" label="休药期(天)" />
        <el-table-column label="操作" width="250">
          <template v-if="scoped.row.drugAlarmConfigId" slot-scope="scoped">
            <div class="table-btns-box">
              <div class="table-btns edit-btns" @click="edit(scoped.row)" v-if="judgePermission(162, 'edit')">
                <p>修改</p>
              </div>
              <div class="table-btns del-btns" @click="delOne(scoped.row)" v-if="judgePermission(162, 'deleteone')">
                <p>删除</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new2.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import DrugAlarmConfigApi from "@/api/health-management/drug-alarm-config";
import MaterialApi from "@/api/basic-info-manage/material";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";

export default {
  name: "EpidemicPreventionManagement",
  components: { ActionBar, Pagination, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data () {
    return {
      // 查询条件：
      fishVarietyId: undefined,
      varietyList: [],
      materialId: undefined, // 药品ID
      materialList: undefined,
      checkedIdList: []
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    this.queryPageList();
    this.queryFishVariety()
    this.queryMaterial()
  },
  mounted () { },
  methods: {
    searchPage(){
      this.currentPage=1
      this.queryPageList();
      setTimeout(() => {
        this.queryPageList();
      }, 200);
    },
    queryFishVariety () {
      FishVarietyApi.queryByCondition().then(res => {
        this.varietyList = res.data;
      })
    },
    queryMaterial () {
      MaterialApi.queryByCondition({ materialTypeNo: 'drug' }).then(res => {
        this.materialList = res.data;
      })
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {}
      };
      if (this.fishVarietyId) params.entity.fishVarietyId = this.fishVarietyId;
      if (this.materialId) params.entity.materialId = this.materialId;
      DrugAlarmConfigApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 单个删除
    delOne (data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        DrugAlarmConfigApi.delById({ id: data.drugAlarmConfigId }).then(res => {
          if (res.success) {
            this.$message.success("删除成功");
            this.queryPageList();
            this.resetActionBar();
          }
        });
      })
        .catch(() => {
          this.resetActionBar()
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage () {
      DrugAlarmConfigApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    changePageSize (pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.drugAlarmConfigId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.drugAlarmConfigId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.drugAlarmConfigId) {
          return item.drugAlarmConfigId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 添加
    add () {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit (row) {
      if (row.drugAlarmConfigId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
